package com.game;

import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.Log;


public abstract class PowerUp {
	public Rect size;
	protected BackgroundMirror mBackgroundMirror;
	protected Point heading;
	protected Bitmap normal, dimm1, dimm2;
	protected int mode; //Normal, dimm1, dimm2, destroyed

	protected int speed;
	protected PlayScene mSceneRef;
	
	PowerUp(Context context, BackgroundMirror bgMirror, Point pos, int imgNormalRef, 
			int imgDimm1Ref, int imgDimm2Ref, PlayScene threadRef)
	{
		mSceneRef = threadRef;
		size = new Rect();
		mBackgroundMirror = bgMirror;
		normal = BitmapFactory.decodeResource(context.getResources(), imgNormalRef);
		dimm1 = BitmapFactory.decodeResource(context.getResources(), imgDimm1Ref);
		dimm2 = BitmapFactory.decodeResource(context.getResources(), imgDimm2Ref);
		mode = 0;
		
		Random rnd = new Random();
		
		heading = new Point(rnd.nextInt(20)-10, rnd.nextInt(20)-10);
		
		size.offset(pos.x, pos.y);
		size.right = normal.getWidth() + size.left;
		size.bottom = normal.getHeight() + size.top;
				
		speed = 2;
	}
	
	public void Colide()
	{
		
		// Wall collision, at the 3rd coll, it dissapears.
		// Calculate collision with walls
		if(getLeft() < mBackgroundMirror.getLeftLimit())
		{
			setLeft(mBackgroundMirror.getLeftLimit());
			heading.x = (-1)*heading.x;
			DimmPowerUp();
			
		}
		else if (getRight() > mBackgroundMirror.getRightLimit())
		{		
			setRight(mBackgroundMirror.getRightLimit());
			heading.x = (-1)*heading.x;
			DimmPowerUp();
			Log.i("GAISMA:: ", " Colide!! :D Mode = " + mode);
		}
		
		if(getTop() < mBackgroundMirror.getTopLimit())
		{			
			setTop(mBackgroundMirror.getTopLimit());
			heading.y = (-1)*heading.y;
			DimmPowerUp();
		}
		else if (getBottom() > mBackgroundMirror.getBottomLimit())
		{
			setBottom(mBackgroundMirror.getBottomLimit());
			heading.y = (-1)*heading.y;
			DimmPowerUp();
		}
		

	}
	
	public void DimmPowerUp()
	{ mode++;}
	
	public void Move()
	{
		if(mode >= 3) return;
		// Continue in the general direction
		float length = (float)Math.sqrt(heading.x*heading.x + heading.y*heading.y);

		float normalizedx = (heading.x/length);
		float normalizedy = (heading.y/length);

		normalizedx *= speed;
		normalizedy *= speed;
		
		size.offset((int)normalizedx, 0);
		size.offset(0, (int)normalizedy);
	}
	
	public void Update()
	{
		if(mode >= 3) return;
		Colide();
		Move();
	}
	
	public void Draw(Canvas canvas)
	{
		if(mode >= 3) return;
		
		switch (mode) 
		{
		case 1:	canvas.drawBitmap(dimm1,size.left, size.top, null);	break;
		case 2:	canvas.drawBitmap(dimm2,size.left, size.top, null);	break;
		default: canvas.drawBitmap(normal,size.left, size.top, null);	break;
		}
		// Draw image
	}
	
	public abstract void Execute();
	
	public void DoPowerUp()
	{
		if(mode >= 3 ) return;
		mode = 3; 
		Execute();
	}
	
	public int getLeft(){ return size.left;}
	public int getRight(){ return size.right;}
	public int getTop(){ return size.top;}
	public int getBottom(){ return size.bottom;}
	
	public void setLeft(int npos){size.left = npos; size.right= npos + normal.getWidth();}
	public void setRight(int npos){size.left = npos - normal.getWidth(); size.right= npos ;}
	public void setTop(int npos){size.top = npos; size.bottom = npos+ normal.getHeight();}
	public void setBottom(int npos){size.top = npos- normal.getHeight(); size.bottom = npos;}
	
	
}
